<?php
	/**
	 * 
	 * @param unknown_type $pMerBillNo
	 * @return string
	 */

require_once(APP_ROOT_PATH.'system/collocation/fuioupay/fuiou_rsa.function.php');
	/**
	 * 创建新帐户
	 * @param int $user_id
	 * @param int $user_type 0:普通用户fanwe_user.id;1:担保用户fanwe_deal_agency.id
	 * @param unknown_type $MerCode
	 * @param unknown_type $cert_md5
	 * @param unknown_type $post_url
	 * @return string
	 */
	function CreateNewAcct($user_id,$platformNo,$post_url,$sys){
		
		$pWebUrl = "http://wap.yzlicai.com/index.php?m=home&c=collocation&a=response_go&class_name=Fuioupay&class_act=CreateNewAcct&from=".strim($_REQUEST['from']);//web方式返回
                  //http://wap.yzlicai.com/index.php?m=home&c=collocation&a=response_go&class_name=Fuioupay&class_act=CreateNewAcct&from=wap
		$uWebUrl = "http://wap.yzlicai.com/index.php?m=home";

		$pS2SUrl = "http://wap.yzlicai.com/index.php?m=home&c=collocation&a=notify&class_name=Fuioupay&class_act=CreateNewAcct&from=".strim($_REQUEST['from']);//s2s方式返回

		$user = array();
		$user = get_user_info("*","id = ".$user_id);

		$fuioupay_log = array();

        if($sys == "pc")
        {
            if($user['user_type']==1){
                $fuioupay_log['code'] = 'toEnterpriseRegister';
                $post_url = $post_url.'webArtifReg.action';
                $company = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."user_company where user_id=".$user_id);
            }else{
                $fuioupay_log['code'] = 'toRegister';
                $post_url = $post_url.'webReg.action';
            }
        }
        else
        {
            if($user['user_type']==1){
                $fuioupay_log['code'] = 'toEnterpriseRegister';
                //company
                $fuioupay_log['code'] = 'toEnterpriseRegister';
                $post_url = 'https://jzh.fuiou.com/webArtifReg.action';
                $company = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."user_company where user_id=".$user_id);
            }else{
                $fuioupay_log['code'] = 'toRegister';
            }
        }
		$fuioupay_log['create_date'] = to_date(TIME_UTC,'Y-m-d H:i:s');
		$GLOBALS['db']->autoExecute(DB_PREFIX."fuioupay_log",$fuioupay_log);
		$requestNo = $GLOBALS['db']->insert_id();
		
		$data = $Sign_data= array();
		$data['requestNo'] = $requestNo;
		$data['create_time'] = TIME_UTC;
		$data['certif_tp'] = 0;
		$Sign_data['ver'] = "0.44";
		$Sign_data['mchnt_cd'] = $platformNo;
		$Sign_data['mchnt_txn_ssn'] = $data['mchnt_txn_ssn'] = get_microtime();//请求流水号
		$Sign_data['user_id_from'] = $data['user_id_from'] = $user_id;//

		if($sys == "pc" || true)
		{
			if($user['user_type']==1){
				$Sign_data['cust_nm'] = $data['cust_nm'] = $company['company_name'];
				$Sign_data['artif_nm'] = $data['artif_nm'] = $user['real_name'];
			}
			else
			{
				$Sign_data['cust_nm'] = $data['cust_nm'] = $user['real_name'];
			}
		}
		else
		{
			$Sign_data['cust_nm'] = $data['cust_nm'] = $user['real_name'];
		}

		$Sign_data['mobile_no'] = $data['mobile_no'] = $user['mobile'];//
 		$Sign_data['certif_id'] = $data['certif_id'] = $user['idno'];//
		$Sign_data['email'] =  $data['email'] = $user['email'];//
		$Sign_data['city_id'] = $data['city_id'] = $user['city_id']='';//开户行地区代码
		$Sign_data['parent_bank_id'] = $data['parent_bank_id'] = $user['parent_bank_id']='';//开户行行别
		$Sign_data['bank_nm'] = $data['bank_nm'] = $user['bank_nm'] ='';//开户行支行名称
		$Sign_data['capAcntNo'] = $data['capAcntNo'] = $user['user_name']='';//开户行账号
		$Sign_data['page_notify_url'] = $pWebUrl;//商户返回地址
		$Sign_data['back_notify_url'] = $pS2SUrl;//$uWebUrl;//商户后台通知地址
		$GLOBALS['db']->autoExecute(DB_PREFIX."fuioupay_register",$data,'INSERT');
		$id = $GLOBALS['db']->insert_id();
		//
		ksort($Sign_data);
		foreach($Sign_data as $k => $v){
			if($k == 'ver'){
				$Sign_old .=$v;
				$Sign_old_k .=$k;
			}else{
				$Sign_old .= $v."|";
				$Sign_old_k .= $k."+|+";
			}
		}
		$a[] = $Sign_old;
		$a[] = $Sign_old_k;
		//return $a;
				
		$pSign=rsaSign($Sign_old,PRIVATE_KEY_PATH);
		
		$strhtml = '';
		foreach($Sign_data as $k => $v){
			$strhtml .= '<input type="hidden" name="'.$k.'" value="'.$v.'" /><br/>';
		}
		$html = '<html><head><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=2.0, user-scalable=0,minimum-scale=0.5"><meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge"><meta http-equiv="content-type" content="text/html; charset=UTF-8" /></head><body>
		<form name="form1" id="form1" method="post" action="'.$post_url.'" target="_self">		
		<input type="hidden" name="signature" value="'.$pSign.'" />
		<input type="hidden" name="certif_tp" value="0" />		
		'.$strhtml.'
		<input type="hidden" value="提交" />
		<div style="width:100%;text-align:center;padding:10px 0;text-align: 18px;"><img src="/public/images/loading.gif" />页面正在跳转，请稍后...</div>
		<div style="width:100%;text-align:center;padding:10px 0;text-align: 18px;"><button type="button" onclick="do_submit()" style="height:50px;padding:10px 20px;text-align: 30px;font-size: 18px;">手动提交</button></div></form></body></html>
		<script language="javascript">
		//document.form1.submit();
		function do_submit(){
		    document.form1.submit();
		}
		</script>';
		//
		$yeepay_log = array();
		$yeepay_log['html'] = $html;
		$GLOBALS['db']->autoExecute(DB_PREFIX."fuioupay_log",$yeepay_log,'UPDATE','id='.$requestNo);
		echo $html;exit;
		return $html;
	
	}
	
	//创建新帐户回调
	function CreateNewAcctCallBack($str3Req){
		$mchnt_txn_ssn = $str3Req["mchnt_txn_ssn"];
		if($mchnt_txn_ssn){
			unset($str3Req["mchnt_txn_ssn"]);
		}
		
		if ($str3Req['code'] == '0000'){
			$where = " mchnt_txn_ssn = '".$mchnt_txn_ssn."'";
			$sql = "update ".DB_PREFIX."fuioupay_register set is_callback = 1 where is_callback = 0 and ".$where;
			$GLOBALS['db']->query($sql);
			if (true){
				$user_id = intval($GLOBALS['db']->getOne("select user_id_from from ".DB_PREFIX."fuioupay_register where ".$where));
				$user_date .= " and city_id = ".$str3Req['city_id'];
				$user_date .= " and parent_bank_id = ". $str3Req['parent_bank_id'];
				$user_date .= " and capAcntNo = ".$str3Req['capAcntNo'];
				$GLOBALS['db']->query("update ".DB_PREFIX."user set ips_acct_no = '".$user_id."',idcardpassed=1,idcardpassed_time='".TIME_UTC."' where id = ".$user_id);
				
				//实名认证
				$GLOBALS['db']->query("update ".DB_PREFIX."user_credit_file set status = 1 , passed = 1 ,passed_time='".TIME_UTC."' where type='credit_identificationscanning' AND user_id = ".$user_id);
				
			}else{
				if (isset($str3Req['message']))
					$str3Req['message'] = '更新is_callback=1失败';
			}
			//操作成功
			$GLOBALS['db']->autoExecute(DB_PREFIX."fuioupay_register",$str3Req,'UPDATE',$where);
			return 1;
		}else{
			return 0;
		}
		
		
	}	
?>